package _09_高频题;

/**
 * https://leetcode-cn.com/problems/move-zeroes/
 * @Author: haogege
 * @Date: 2021/9/1
 */
public class _283_移动零 {

    // 核心思路，找到非0元素，和指针交换位置
    public void moveZeroes(int[] nums) {
        int storeIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                if (storeIndex != i) {
                    nums[storeIndex] = nums[i];
                    nums[i] = 0;
                }
                storeIndex++;
            }
        }
    }

    // 核心思路，找到非0元素，和指针交换位置
    public void moveZeroes1(int[] nums) {
        int storeIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                // 和storeIndex位置的元素交换位置
                int temp = nums[storeIndex];
                nums[storeIndex++] = nums[i];
                nums[i] = temp;
            }
        }
    }

}
